package mindteck.store;

import java.util.ArrayList;
import java.util.List;

public class ShoppingCart {
	
	private int cartId;
	private List <Item> itemList;
	private String dateCreated;
	private double total;
	
	public ShoppingCart(){
		itemList = new ArrayList<Item>();
	}
	
	public int getCartId(){
		return cartId;
	}
	
	public String getDateCreated(){
		return dateCreated;
	}
	
	public void setCartId(int cartId){
		this.cartId = cartId;
	}
	
	public void setDateCreated(String dateCreated){
		this.dateCreated = dateCreated;
	}
	
	public void setTotal(double total){
		this.total = total;
	}
	
	public double getTotal(){
		return total;
	}
	/*Add item to list*/
	public void addItem(Item item){
		itemList.add(item);		
	}
	
	/*Iterate through the list of items and remove from list item with same name 
	 * as item passed in. Return boolean */
	public boolean deleteItem(Item item){
		boolean t = false;
		
		for(Item temp:itemList)
			if (temp.getItemBean().getName().equals (item.getItemBean().getName())  )
			     t = itemList.remove(temp);	
		return t;
	}
	
	/*return list of items in cart*/
	public List<Item> viewCart(){
		return itemList;
	}
	
	/*total dollar amount of items in cart*/
	public double calculateTotal(){
		double sum = 0;
		for(Item temp:itemList){
			sum += temp.getItemBean().getSubtotal();
		}
		return sum;
	}
	
	
	/*Update quantity of an item in cart*/
	public void updateQuantity(Item item, int newQuantity){
		for(Item temp:itemList){
			if (temp.getItemBean().getName().equals (item.getItemBean().getName())  ){
				int index = itemList.indexOf(temp);	
				/*set new quantity*/
				itemList.get(index).getItemBean().setQuantity(newQuantity);
			}
		}
	}
	
	public void checkOut(){
		
		/*calculate shopping cart total*/
		setTotal(calculateTotal());
	}
	
}
